/* Copyright (C) 2010 Luca Piccinelli
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/* 
 * File:   adv_pipeline_data_structures.h
 * Author: picci
 *
 * Created on November 6, 2010, 3:24 PM
 */

#ifndef ADV_PIPELINE_DATA_STRUCTURES_H
#define	ADV_PIPELINE_DATA_STRUCTURES_H

#include "prj_definitions.h"
#include "pipeline/pipeline_data_structures.h"

namespace NAMESPACE{

// ****** ConditionalPipeStep definition **************************************************
template<typename Predicate>
class ConditionalPipeStep : public PipeStep{
public:
    typedef Predicate condition_t;

protected:
    const condition_t* condition;

public:
    ~ConditionalPipeStep(){}
    ConditionalPipeStep() : PipeStep(), condition(0){}
    ConditionalPipeStep(const ConditionalPipeStep& lps) : PipeStep(lps),
                                            condition(lps.condition){}
    ConditionalPipeStep& operator=(const ConditionalPipeStep& lps){
        if(this == &lps) return *this;
        PipeStep::operator=(lps);
        condition = condition;
        return *this;
    }

    ConditionalPipeStep(const condition_t& _condition) : PipeStep(), condition(&_condition){}

    virtual bool execute() throw(bad_computation, bad_io_mapping, gestface_exception){
        if((*condition)()) if( !PipeStep::execute() ) return false;
        return true;
    }

    ConditionalPipeStep& set_condition(const condition_t& _condition){
       condition = &_condition;
       return *this;
    }
};
// -----------------------------------------------------------------------------

// ****** LoopPipeStep definition **************************************************
template<typename Predicate>
class LoopPipeStep : public ConditionalPipeStep<Predicate>{
public:
    typedef Predicate condition_t;

private:
    using ConditionalPipeStep<condition_t>::condition;
    counter_t loop_counter;

public:
    
    ~LoopPipeStep(){}
    LoopPipeStep() : ConditionalPipeStep<Predicate>(),
                     loop_counter(0){}
    LoopPipeStep(const LoopPipeStep& lps) : ConditionalPipeStep<Predicate>(lps),
                                            loop_counter(lps.loop_counter){}
    LoopPipeStep& operator=(const LoopPipeStep& lps){
        if(this == &lps) return *this;
        ConditionalPipeStep<Predicate>::operator=(lps);
        loop_counter = lps.loop_counter;
        return *this;
    }

    LoopPipeStep(const condition_t& _condition) : ConditionalPipeStep<Predicate>(_condition),
                                                  loop_counter(0){}

    bool execute() throw(bad_computation, bad_io_mapping, gestface_exception){
        while((*condition)()){
            if(!PipeStep::execute()) return false;
            loop_counter++;
        }
        return true;
    }

    const counter_t& get_loops() const{
        return loop_counter;
    }
};
// -----------------------------------------------------------------------------

}

#endif	/* ADV_PIPELINE_DATA_STRUCTURES_H */

